package com.seed.application;

import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.datasource.DataSourceKey;
import com.seed.application.context.SeedContext;
import com.seed.application.context.impl.SeedContextImpl;
import com.seed.application.datasourcekey.DataSourceKeyContext;
import com.seed.core.action.Action;
import com.seed.core.configuration.AppConfiguration;
import com.seed.core.dialect.DialectDDL;
import com.seed.core.enumeration.AppMode;
import com.seed.core.query.App;
import com.seed.core.query.Micro;
import com.seed.fileud.Fileud;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.File;

@Slf4j
public class Sa {

    private static File file; // 应用配置地址，如： E:\seed-dev-path\first.json
    private static File path; // 开发的配置路径， 如： E:\seed-dev-path
    private static AppMode mode; // 应用模式： dev, single, app, micro

    private static AppConfiguration configuration;

    private static Environment environment;

    private static DialectDDL ddl;

    private static SeedContext seedContext;
    private static DataSourceKeyContext dataSourceKeyContext;
    private static ApplicationContext applicationContext;

    public static Action getAction(RequestMethod method, String actionName) {
        return Fileud.getAction(method, actionName);
    }

    public static File getFile() {
        return file;
    }

    public static void setFile(File file) {
        Sa.file = file;
    }

    public static File getPath() {
        return path;
    }

    public static void setPath(File path) {
        Sa.path = path;
    }

    public static AppMode getMode() {
        return mode;
    }

    public static void setMode(AppMode mode) {
        Sa.mode = mode;
    }

    public static AppConfiguration getConfiguration() {
        return configuration;
    }

    public static void setConfiguration(AppConfiguration configuration) {
        Sa.configuration = configuration;
    }

    public static Environment getEnvironment() {
        return environment;
    }

    public static void setEnvironment(Environment environment) {
        Sa.environment = environment;
    }

    public static DialectDDL getDdl() {
        return ddl;
    }

    public static void setDdl(DialectDDL ddl) {
        Sa.ddl = ddl;
    }

    public static SeedContext getSeedContext() {
        return seedContext;
    }

    public static void setSeedContext() {
        Sa.seedContext = new SeedContextImpl();
    }

    public static String getDataSourceKey(String tenantname) {
        return dataSourceKeyContext.getResult(tenantname);
    }

    public static void setDataSourceKeyContext() {
        Sa.dataSourceKeyContext = new DataSourceKeyContext(mode);
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static void setApplicationContext(ApplicationContext applicationContext) {
        Sa.applicationContext = applicationContext;
    }

    public static <T> T getBean(Class<T> type) throws BeansException {
        return applicationContext.getBean(type);
    }

    public static App getApp() {
        return seedContext.getApp();
    }

    public static Micro getMicro() {
        return seedContext.getMicro();
    }

    /**
     * 切换到内部库
     */
    public static void switchDataSourceKey() {
        String dataSourceKey = getDataSourceKey(null);
        if(StrUtil.isNotBlank(dataSourceKey)) {
            log.info("切换到数据源【{}】", dataSourceKey);
            DataSourceKey.use(dataSourceKey);
        }
    }
}
